package com.mbl.base.modular.mall.service.impl;

import com.mbl.base.modular.mall.entity.GoodsProduct;
import com.mbl.base.modular.mall.mapper.GoodsProductMapper;
import com.mbl.base.modular.mall.service.IGoodsProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 商品货品表 服务实现类
 * </p>
 *
 * @author lwj
 * @since 2020-02-27
 */
@Service
public class GoodsProductServiceImpl extends ServiceImpl<GoodsProductMapper, GoodsProduct> implements IGoodsProductService {

    @Override
    public int reduceStock(Long productId, Integer number) {
        final GoodsProduct goodsProduct = super.getById(productId);
        final int numberT = goodsProduct.getNumber() - number;
        if(numberT < 0){
            return 0;
        }
        goodsProduct.setNumber(numberT);
        goodsProduct.setUpdateTime(LocalDateTime.now());
        super.updateById(goodsProduct);
        return 1;
    }

    @Override
    public int addStock(Long productId, Integer number) {
        final GoodsProduct goodsProduct = super.getById(productId);
        final int numberT = goodsProduct.getNumber() + number;
        goodsProduct.setNumber(numberT);
        goodsProduct.setUpdateTime(LocalDateTime.now());
        super.updateById(goodsProduct);
        return 1;
    }
}
